package mb.model;

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import mb.view.AppToolBar;  // +

public class Diagram extends JPanel
  implements MouseListener, MouseMotionListener {

  public static final Color DIAGRAM_COLOR = Color.WHITE;

  private Point mousePressed = new Point(0, 0);

  private	AppToolBar appToolBar;  // +

  private Diagram() {  // &
    super();
    this.addMouseListener(this);
    this.setBackground(DIAGRAM_COLOR);
    this.setLayout(null);
  }

  // {+
  public Diagram(AppToolBar aAppToolBar) {
    this();
    this.setAppToolBar(aAppToolBar);
  }

  public AppToolBar getAppToolBar() {
    return appToolBar;
  }

  public void setAppToolBar(AppToolBar aAppToolBar) {
  	appToolBar = aAppToolBar;
  }
  // +}

  private void addNewBox(Point aPoint) {
    Box newBox = new Box();
    newBox.setLocation(aPoint);
    newBox.addMouseListener(this);
    newBox.addMouseMotionListener(this);
    this.add(newBox);
    this.repaint();
  }

  public void selectAllBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        box.setSelected(true);
      }
    }
  }

  public void deSelectAllBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        box.setSelected(false);
      }
    }
  }

  public void hideSelectedBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        if (box.isSelected()) {
          box.setVisible(false);
        }
      }
    }
  }

  public void showSelectedBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        if (box.isSelected()) {
          box.setVisible(true);
        }
      }
    }
  }

  public Dimension getPreferredSize() {
    return new Dimension(1600, 1600);
  }

  public void mousePressed(MouseEvent e) {
    if (e.getSource() == this) {
      if (appToolBar.getSelectedButton() == AppToolBar.BOX) {  // +
        this.addNewBox(e.getPoint());
      }  // +
      else if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {  // +
        this.deSelectAllBoxes();  // +
      }  // +
    }
    else if (e.getSource() instanceof Box) {
      if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {  // +
        Box clickedBox = (Box) (e.getSource());
        clickedBox.setSelected(!(clickedBox.isSelected()) );
        mousePressed = e.getPoint();
      }  // +
    }
  }

  public void mouseClicked(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

  public void mouseDragged(MouseEvent e) {
    if (appToolBar.getSelectedButton() == AppToolBar.SELECT) {  // +
      if (e.getSource() instanceof Box) {
        Box box = (Box) e.getSource();
        // box location (x, y) and size (r) before the box has been dragged
        Rectangle r = box.getBounds();
        int x = r.x + e.getX() - mousePressed.x;
        int y = r.y + e.getY() - mousePressed.y;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        box.setLocation(x, y);
      }
    }  // +
  }

  public void mouseMoved(MouseEvent e) {}

}
